package com.corpgovernment.common.base;

import com.ctrip.corp.obt.generic.security.annotation.SecurityEntity;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.ToString;

import java.util.List;

/**
 * @author majl
 * @date 2019-5-21
 * @Description: 自定义响应数据结构
 * status 200：表示成功
 */
@ToString
public class JSONResult<T> {

    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    static {
        MAPPER.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
    }

    // 响应业务状态
    private Integer status;

    // 响应消息
    private String msg;

    // 响应中的数据
    @SecurityEntity
    private T data;

    public JSONResult() {

    }

    public JSONResult(Integer status, String msg, T data) {
        this.status = status;
        this.msg = msg;
        this.data = data;
    }

    /**
     * @Author: majl
     * @Date: 2019-5-20
     * @Description: 默认成功状态 status:200 msg:SUCCESS
     */
    public JSONResult(T data) {
        this.status = 200;
        this.msg = "SUCCESS";
        this.data = data;
    }

    public static <T> JSONResult<T> build(Integer status, String msg, T data) {
        return new JSONResult<T>(status, msg, data);
    }

    public static <T> JSONResult<T> success(T data) {
        return new JSONResult<>(data);
    }

    public static <T> JSONResult<T> ok() {
        return new JSONResult<>(null);
    }

    public static <T> JSONResult<T> errorMsg(String msg) {
        return new JSONResult<>(500, msg, null);
    }

    public static <T> JSONResult<T> errorMsg(int status, String msg) {
        return new JSONResult<>(status, msg, null);
    }

    public static <T> JSONResult<T> errorMap(T data) {
        return new JSONResult<>(501, "error", data);
    }

    public static <T> JSONResult<T> errorTokenMsg(String msg) {
        return new JSONResult<>(502, msg, null);
    }

    public static <T> JSONResult<T> errorException(String msg) {
        return new JSONResult<>(555, msg, null);
    }

    /**
     * @param jsonData
     * @param clazz
     * @return
     * @Description: 将json结果集转化为JSONResultUtil对象
     * 需要转换的对象是一个类
     */
    public static JSONResult formatToPojo(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                return MAPPER.readValue(jsonData, JSONResult.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (clazz != null) {
                if (data.isObject()) {
                    obj = MAPPER.readValue(data.traverse(), clazz);
                } else if (data.isTextual()) {
                    obj = MAPPER.readValue(data.asText(), clazz);
                }
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param json
     * @return
     * @Description: 没有object对象的转化
     */
    public static JSONResult format(String json) {
        try {
            return MAPPER.readValue(json, JSONResult.class);
        } catch (Exception e) {
        }
        return null;
    }

    /**
     * @param jsonData
     * @param clazz
     * @return
     * @Description: Object是集合转化
     * 需要转换的对象是一个list
     */
    public static JSONResult formatToList(String jsonData, Class<?> clazz) {
        try {
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (data.isArray() && data.size() > 0) {
                obj = MAPPER.readValue(data.traverse(),
                        MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
            }
            return build(jsonNode.get("status").intValue(), jsonNode.get("msg").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * result 是否为空
     *
     * @param result
     * @return
     */
    public static Boolean checkResult(JSONResult result) {
        return result != null && result.getData() != null;
    }

    public Boolean isSUCCESS() {
        return this.status == 200;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
